﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Table_Dal;
namespace DAL
{
    public class DapAnDal:DataAccessLayer
    {
        DataDataContext dt;
        public List<DapAnTbl> DapAnCauHoi_TheoMacauHoi_TenCauHoi(string MaCauHoi,string TenCauhoi)
        {
            dt = GetKetnoi();
            var q = dt.DapAnCauHoi_TheoMacauHoi_TenCauHoi(MaCauHoi, TenCauhoi);
            List<DapAnTbl> DsDapAn = new List<DapAnTbl>();
            foreach (var item in q)
            {
                DapAnTbl DapAn = new DapAnTbl();
                DapAn.HinhAnh = item.HINHANH;
                DapAn.LoaiDA = int.Parse(item.LOAIDA.ToString());
                DapAn.MaDapAn = item.MADAPAN;
                DapAn.NoiDung = item.NOIDUNG;
                DsDapAn.Add(DapAn);
            }
            return DsDapAn;
        }
        public bool Traloi(string Macauhoi, List<TraloiThi> ds)
        {
            dt = GetKetnoi();
            IEnumerable<DAPAN> dsdapan = dt.DAPANs.Where(k => k.MACAUHOI == Macauhoi);
            int x = 0;
            int y = 0;
            foreach (DAPAN item in dsdapan)
            {
                foreach (TraloiThi tl in ds)
                {
                    if (tl.MaDapAn == item.MADAPAN && tl.LoaiDA == int.Parse(item.LOAIDA.ToString()))
                    {
                        x++;
                    }
                }
                y++;
            }
            if (x == y) return true;
            else return false;
        }
        public int TongSoDapAnDung(string macauhoi)
        {
            dt = GetKetnoi();
            IEnumerable<DAPAN> ds = dt.DAPANs.Where(k => k.MACAUHOI == macauhoi).Where(j => j.LOAIDA == 1);
            return ds.Count();
        }
        public List<TraloiThi> DanhsachDapAn_Thi(string MaCauHoi)
        {
            dt = GetKetnoi();
            IEnumerable<DAPAN> ds = dt.DAPANs.Where(j => j.MACAUHOI == MaCauHoi);
            List<TraloiThi> DsDapAn = new List<TraloiThi>();
            foreach (DAPAN item in ds)
            {
                TraloiThi DapAn = new TraloiThi();
                DapAn.MaCauHoi = MaCauHoi;
                DapAn.HinhAnh = item.HINHANH;
                DapAn.LoaiDA = 0;
                DapAn.MaDapAn = item.MADAPAN;
                DapAn.NoiDung = item.NOIDUNG;
                DsDapAn.Add(DapAn);
            }
            return DsDapAn;
        }
        public int SoluongCauHoi_TheoMaCauhoi(string Macauhoi)
        {
            dt = GetKetnoi();
            return dt.SoluongDapan_TheoMacauhoi(Macauhoi);
        }
        public bool ThemDapAn(DapAnTbl Dapan)
        {
            try
            {
                dt = GetKetnoi();
                DAPAN da = new DAPAN();
                string hinhanh = "";
                if (Dapan.HinhAnh != null)
                    hinhanh = Dapan.HinhAnh;
                da.HINHANH = hinhanh;
                da.MADAPAN = Dapan.MaDapAn;
                da.MACAUHOI = Dapan.MaCauHoi;
                da.NOIDUNG = Dapan.NoiDung;
                da.LOAIDA = Dapan.LoaiDA;
                dt.DAPANs.InsertOnSubmit(da);
                dt.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool SuaDapAn(DapAnTbl Dapan)
        {
            try
            {
                dt = GetKetnoi();
                DAPAN da = dt.DAPANs.SingleOrDefault(i => i.MADAPAN == Dapan.MaDapAn);
                da.NOIDUNG = Dapan.NoiDung;
                da.HINHANH = Dapan.HinhAnh;
                da.LOAIDA = Dapan.LoaiDA;
                dt.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool XoaDapAn(string MaDapAn)
        {
            try
            {
                dt = GetKetnoi();
                DAPAN da = dt.DAPANs.SingleOrDefault(i => i.MADAPAN == MaDapAn);
                dt.DAPANs.DeleteOnSubmit(da);
                dt.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool XoaTatca_MaCauHoi(string MacauHoi)
        {
            try
            {
                dt = GetKetnoi();
                IEnumerable<DAPAN> ds = dt.DAPANs.Where(k => k.MACAUHOI == MacauHoi);
                dt.DAPANs.DeleteAllOnSubmit(ds);
                dt.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public string PictureLink(string MaDapAn)
        {
            dt = GetKetnoi();
            DAPAN da = dt.DAPANs.SingleOrDefault(i => i.MADAPAN == MaDapAn);
            if (da.HINHANH == "" || da.HINHANH == null)
                return "";
            else
                return da.HINHANH;
        }

        public bool KiemTra_TrungKhoa(string Ma)
        {
            dt = GetKetnoi();
            IEnumerable<DAPAN> q = dt.DAPANs.Where(k => k.MADAPAN == Ma);
            foreach (DAPAN item in q)
            {
                if (item.MADAPAN == Ma)
                    return true;
            }
            return false;
        }

        public List<string> LayTatCa_string()
        {
          
            dt = GetKetnoi();
            IEnumerable<DAPAN> ds = dt.DAPANs;
            List<string> dslst = new List<string>();
            foreach (DAPAN item in ds)
            {
                dslst.Add(item.MADAPAN);  
            }

            return dslst;
       
        }
    }
}
